package com.tanguy.user.ui.trip.activity.choice

import android.app.Activity
import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import com.tanguy.user.Const
import com.tanguy.user.R
import com.tanguy.user.base.ImmersionTitleActivity
import com.tanguy.user.bean.station.CarBean
import com.tanguy.user.bean.station.SiteBean
import com.tanguy.user.bean.station.StationBean
import com.tanguy.user.net.http.HttpManager
import com.tanguy.user.ui.trip.activity.adapter.StationAdapter
import com.tanguy.user.utlis.extend.click
import com.tanguy.user.utlis.extend.getTitleText
import com.tanguy.user.utlis.extend.requestSucceed
import org.jetbrains.anko.find
import org.jetbrains.anko.startActivityForResult

/**
 * Created by dab on 2017/11/20 0020 15:41
 * 选择站点
 * 需要Const.IntentCode.CITY_NAME
Const.IntentCode.CITY_CODE
 */
class SelectStationActivity : ImmersionTitleActivity() {
    override fun setContentViewRes(): Int = R.layout.activity_select_station
    val data = ArrayList<SiteBean>()
    lateinit var cityCode: String
    private var stationBean: StationBean? = null
    var carBeanList: ArrayList<CarBean>? = null
    private val stationAdapter by lazy { StationAdapter(data) }

    override fun initView() {
        super.initView()
        val cityName = intent.getStringExtra(Const.IntentCode.CITY_NAME)
        cityCode = intent.getStringExtra(Const.IntentCode.CITY_CODE)
        title=getTitleText(cityName)
        find<RecyclerView>(R.id.rv_content).apply {
            adapter = stationAdapter
            layoutManager = LinearLayoutManager(this@SelectStationActivity)
        }
    }

    override fun initEvent() {
        super.initEvent()
        click(titleBar.titleView) {
            if (stationBean == null) {
                return@click
            }
            startActivityForResult<ServiceCityActivity>(Const.RequestCode.SERVICE_CITY
                    , Const.IntentCode.CITY_BEAN to stationBean!!
            )
        }
        stationAdapter.setOnItemClick {
            _, _, data ->
            setResult(Activity.RESULT_OK, Intent()
                    .putParcelableArrayListExtra(Const.IntentCode.CAR_LIST, carBeanList)
                    .putExtra(Const.IntentCode.SITE_BEAN, data)
            )
            finish()

        }

    }



    private fun load(cityCode: String) {
        HttpManager.origin(cityCode)
                .requestSucceed(this) {
                    data.clear()
                    stationBean = it
                    var site = it.site
                    if (site == null)
                        site = ArrayList<SiteBean>()
                    stationAdapter.setLoadData(site, false)
                }
    }

    override fun initData() {
        super.initData()
        load(cityCode)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == Const.RequestCode.SERVICE_CITY && data != null) {
                val cityName = data.getStringExtra(Const.IntentCode.CITY_NAME)
                title=getTitleText(cityName)
                val cityID = data.getStringExtra(Const.IntentCode.CITY_ID)
                HttpManager.getSiteList(cityID)
                        .requestSucceed(this) {
                            this.data.clear()
                            stationAdapter.setLoadData(it.site, false)
                            carBeanList = it.car
                        }
            }
        }
    }
}